﻿using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;

public class ToolTip : ShowHideBehaviour {

    private Image BackgroundImage;
    private Text BackgroundText;
    private Text TitleText;
    private Text ContentText;
    private float Smoothing = 5;
    private float LocationOffset = 20;
    private float WidthOffset = 60;
    private float HeightOffset = 60;
    private Vector2 ShowOffset;
    private ArticleSlot HoverArticleSlot;

    // Use this for initialization
    void Start () {
        base.InitShowHide();
        this.BackgroundText = GetComponent<Text>();
        this.TitleText = this.transform.Find("Title").GetComponent<Text>();
        this.BackgroundImage = this.transform.Find("Background").GetComponent<Image>();
        this.ContentText = this.BackgroundImage.transform.Find("Text").GetComponent<Text>();
	}
	
	// Update is called once per frame
	void Update () {
        base.HandleShowHide(this.Smoothing);
	}

    /// <summary>
    /// 显示
    /// </summary>
    /// <param name="title"></param>
    /// <param name="content"></param>
    /// <param name="position"></param>
    public void Show(string title, string content, Vector2 position) {
        this.Refresh(title, content);
        this.transform.position = position + this.ShowOffset;
        base.Show();
    }

    public void Show(ArticleSlot articleSlot, Vector2 position) {
        this.HoverArticleSlot = articleSlot;
        Article article = articleSlot.ArticleInfo.Article;
        this.Refresh(article);
        this.transform.position = position + this.ShowOffset;
        base.Show();
    }

    /// <summary>
    /// 刷新显示信息
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="content">内容</param>
    public void Refresh(string title, string content) {
        this.BackgroundText.text = "\n" + content;
        this.TitleText.text = title;
        this.ContentText.text = "\n" + content;
        this.AdjustOffset();
    }

    public void Refresh(Article article) {
        Personage personage = GlobalData.Instance.GetPlayerPersonage();
        List<Constant.RequireCategory> notMeetRequireCategorys = article.AbleEquip(personage);
        string intro = article.Intro;
        if (notMeetRequireCategorys.Count > 0) {
            foreach (Constant.RequireCategory requireCategory in notMeetRequireCategorys) {
                intro = intro.Replace("{" + requireCategory + "}", Constant.ArticleNotMeetConditionsColor); // 替换为不满足条件的颜色
            }
            string pattern = @"\\{.+\\}";
            intro = Regex.Replace(intro, pattern, Constant.ArticleMeetConditionsColor); // 替换为满足条件的颜色
        }
        this.Refresh(article.Name, intro);
    }

    /// <summary>
    /// 根据最后进入的格子刷新信息
    /// </summary>
    public void Refresh() {
        Article article = this.HoverArticleSlot.ArticleInfo.Article;
        if (article != null) {
            this.Refresh(article);
        } else {
            base.Hide();
        }
    }

    /// <summary>
    /// 跟随鼠标移动
    /// </summary>
    /// <param name="canvas"></param>
    public void FollowMouse(Canvas canvas) {
        if (base.IsShowing()) {
            this.AdjustOffset();
            UIHelper.FollowMouse(canvas, this, this.ShowOffset);
        }
    }

    private void AdjustOffset() {
        float widthOffset;
        float heightOffset;
        float ratio = Screen.width / 1920f; // 坐标像素比
        float width = this.BackgroundText.preferredWidth;
        float height = this.BackgroundText.preferredHeight;
        if (Input.mousePosition.x + (width + this.WidthOffset + this.LocationOffset) * ratio > Screen.width) { // 右边显示超出屏幕了
            widthOffset = -width - this.WidthOffset;
        } else { // 右边显示未超出屏幕
            widthOffset = this.WidthOffset;
        }
        if ((height + this.HeightOffset + this.LocationOffset) * ratio > Input.mousePosition.y) { // 下边显示超出屏幕了
            heightOffset = height + this.HeightOffset;
        } else { // 下边显示未超出屏幕
            heightOffset = -this.HeightOffset;
        }
        this.ShowOffset = new Vector2(widthOffset, heightOffset);
    }
}
